package zj.com.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import zj.com.model.DjcModel;
import zj.com.model.InspectContent;
import zj.com.util.LogUtils;
import zj.com.zj.R;

/**
 * Created by xi on 16/6/3.
 */
public class JcxDetailAdapter extends BaseAdapter{

    public List<InspectContent> list;
    public Context context;
    public LayoutInflater inflater;

    public JcxDetailAdapter(List<InspectContent> list, Context context){

        this.list = list;
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = inflater.inflate(R.layout.adapter_jcx_detail_item, null);
            viewHolder.name_tv = (TextView) convertView.findViewById(R.id.name_value_tv);

            viewHolder.des_tv = (TextView) convertView.findViewById(R.id.des_value_tv);
            viewHolder.fen_tv = (TextView) convertView.findViewById(R.id.fen_value_tv);
            viewHolder.risk_value_tv = (TextView) convertView.findViewById(R.id.risk_value_tv);
            viewHolder.view = convertView;
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }



        if(position % 2 == 0){
            viewHolder.view.setBackgroundColor(Color.WHITE);
        }else{

            viewHolder.view.setBackgroundColor(Color.parseColor("#fdfdff"));
        }
        InspectContent content  = list.get(position);
        viewHolder.name_tv.setText(content.getInspectContentName());
        viewHolder.risk_value_tv.setText(content.getRiskTypeName());
        viewHolder.des_tv.setText(content.getDes());
        viewHolder.fen_tv.setText(content.getPoints()+"");

        ViewTreeObserver observer =  viewHolder.name_tv.getViewTreeObserver();
        observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

            @Override
            public void onGlobalLayout() {

                int  heigh = viewHolder.name_tv.getLineHeight()*viewHolder.name_tv.getLineCount()+20;
                LogUtils.getInstance().error("height====="+heigh);
                viewHolder.name_tv.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                ViewGroup.LayoutParams lp = viewHolder.name_tv.getLayoutParams();
                lp.height = heigh;
                viewHolder.name_tv.setLayoutParams(lp);

            }
        });
        return convertView;
    }
    private static class ViewHolder {
        TextView name_tv;
        TextView des_tv;
        TextView fen_tv,risk_value_tv;
        View view;


    }

    public void setDataChange(List<InspectContent> list){

        this.list = list;
        notifyDataSetChanged();
    }
}
